﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LearnDrawing.Controls
{
    /// <summary>
    /// WithShaps.xaml 的交互逻辑
    /// </summary>
    public partial class WithShaps : UserControl
    {
        public WithShaps()
        {
            InitializeComponent();
        }


        public string DrawingTime
        {
            get { return (string)GetValue(DrawingTimeProperty); }
            set { SetValue(DrawingTimeProperty, value); }
        }

        public static readonly DependencyProperty DrawingTimeProperty =
            DependencyProperty.Register("DrawingTime", typeof(string), typeof(WithShaps), new UIPropertyMetadata("", null));



        public WaveData WaveData
        {
            get { return (WaveData)GetValue(WaveDataDataProperty); }
            set { SetValue(WaveDataDataProperty, value); }
        }

        public static readonly DependencyProperty WaveDataDataProperty =
            DependencyProperty.Register("WaveData", typeof(WaveData), typeof(WithShaps), new UIPropertyMetadata(null, WaveDataPropertyChanged));

        private static void WaveDataPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs arg)
        {
            WithShaps? control = obj as WithShaps;
            WaveData Data = (WaveData)arg.NewValue;

            if (Data != null)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                control?.DrawWave(Data);
                stopwatch.Stop();
                control.DrawingTime = $"Using: {stopwatch.ElapsedMilliseconds}ms";
            }
        }

        private void DrawWave(WaveData Data)
        {
            this.MainCanvas.Children.Clear();

            for (int i = 0; i < 1900; i++)
            {
                int x1 = i;
                int y1 = Data.Datas[i];               

                Ellipse ellipse = new Ellipse()
                {
                    Width = 60,
                    Height = 60,
                    Margin = new Thickness(x1/2, y1, 0, 0),
                    StrokeThickness = 1,
                    Stroke = new SolidColorBrush(Colors.LightGreen)
                };              

                this.MainCanvas.Children.Add(ellipse);
            }
        }       
    }
}
